package com.ksyun.alert.utils.AggregateOperations;

import com.ksyun.alert.utils.AggregateOperations.Params.RateParam;
import com.ql.util.express.Operator;

import java.util.List;

/**
 * Rate聚合运算符
 * @author: hantou
 * @date: 2024-05-05
 */
public class RateOperator extends Operator {

    @Override
    public Object executeInner(Object[] objects) {
        Object object1 = objects[0];
        if (object1 instanceof java.util.List){
            // 这里根据rate的计算方法，是否可以只传入头尾两个状态？ 每个状态分别包含对应的时间戳和值
            List<RateParam> list = (List<RateParam>) object1;
            // 这里时间先->后
            int size = list.size();
            RateParam startParam = list.get(0);
            RateParam endParam = list.get(size - 1);
            return (endParam.getValue() - startParam.getValue()) / (endParam.getTimestamp() - startParam.getTimestamp());
        }
        return null;
    }
}
